import Vue from 'vue'
import Router from 'vue-router'
import {routes} from  '@/router/routers'
import {getCookie,removeCookie} from  "@/libs/cookie"
Vue.use(Router)
const RouterConfig = {
  mode:'hash',
  routes:routes,
  isAddDynamicMenuRoutes:false
}
const router = new Router(RouterConfig);
//路由拦截
router.beforeEach((to,from,next) => {
  if(to.name === 'login'){ //前往登录页
    removeCookie('token');
    removeCookie('user');
    next()
  }else{
    if(getCookie('token')){//若token存在则进入
      next()
     }else{//token不存在,重新登录
      next({name:'login'})
    }
  }
});
export default router;

